<?php
  /**
   * @author Evandro de Oliveira.
   * 
   * @category HELPER.
   * 
   * @since 09/09/2010.
   * 
   * @copyright 2009.
   * 
   * @version 1.0.
   */

  class Zend_View_Helper_DbComboBox extends Zend_View_Helper_Abstract{
    public static function dbComboBox($strName, $strId = '', $strModel, $strValue, $strLabel, $strVSelected = '', $strLSelected='', $boolEnabled = true, $strOrder = '', $join = null, $arrWhere = '', $strCompare = '=', $strEvent = null){
      $strModel = 'Application_Model_' . $strModel;
    	$objDao = new $strModel();
    	$strSql = $objDao->select();
      if($arrWhere == '')
      	$strSql->where('1 = ?', 1); 
      else
        foreach($arrWhere as $strKey => $strVal)
          $strSql->where($strKey . ' ' . $strCompare . ' ?', $strVal);
      
      if($strOrder)
        $strSql->order($strOrder);
        
      $arrResult = $objDao->fetchAll($strSql)->toArray();
      
      if($strId == ''){
        $strId = $strName;
      }
      
      if(!$boolEnabled)
        $strEnabled = 'disabled="disabled" style="background-color:#F5F5F5; border:solid 1px #CDCDCD;"';
      
      $strSelect = "
        <select name='{$strName}' id='{$strId}' {$strEvent} class='select' {$strEnabled}>
          <option value='{$strVSelected}'>" . utf8_encode($strLSelected) . "</option>"; 
          foreach($arrResult as $arrLine){
            $strSelectedValue = '';
            if($strVSelected != '' && $arrLine[$strValue] == $strVSelected ){
              $strSelectedValue = 'selected';
            }
            
            if($strVSelected != $arrLine[$strValue]){
              $strSelect .= "
                <option value='{$arrLine[$strValue]}' " . $strSelectedValue . ">" . 
                  utf8_encode($arrLine[$strLabel]) . "
                </option>
              ";
            }
          }
          $strSelect .= "
        </select>
      ";
          
      return $strSelect;     
    }
  } 
?>